<?php


namespace App\Models\Admin;


use App\Log\AdminImgLog;

class AdminImg extends Base
{
    const OPERATE_PID = 4;
    const MODEL = 'admin_img';

    protected $table = 'admin_img';
    protected $guarded = [];
    public $timestamps = false;

    public static function getList($data=[],$skip=0,$size=10)
    {
        $select=self::getWhere($data);
        $list=$select->orderBy('id','desc')
        ->skip($skip)->take($size)
        ->get()->transform(function ($i)
            {
                $i->img=getOSSUrl($i->img);
                return $i;
            });
        return $list;
    }

    public static function getWhere($data)
    {
        $select=self::where('status',1);
        if(isset($data['category_id']))
        {
            $select=$select->where('category_id',$data['category_id']);
        }
        return $select;
    }

    public static function getCount($data)
    {
        $select=self::getWhere($data);
        $count=$select->count();
        return $count;
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new AdminImgLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }
}
